﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Innovation
{
    public class Actor : Component, I3DComponent
    {
        // The model to draw
        IEModel model;
        public IEModel ModelValue
        {
            get { return model; }
            set { model = value; }
        }

        string modelName;
        public string Model
        {
            get { return modelName; }
            set
            {
                modelName = value;
                model.Model = value;
            }
        }

        // I3DComponent values
        protected Transform transform = new Transform();
        public Transform Transform { get { return transform; } }

        public virtual BoundingBox BoundingBox
        {
            get
            {
                return new BoundingBox(
                    Transform.Position - (Transform.Scale / 2),
                    Transform.Position + (Transform.Scale / 2)
                );
            }
        }

        public Actor() { }

        // Constructors take a model to draw and a position
        public Actor(string Model, Vector3 Position) : base()
        {
            Setup(Model, Position);
        }

        public Actor(string Model, Vector3 Position, GameScreen Parent)
            : base(Parent)
        {
            Setup(Model, Position);
        }

        // Provide a method to setup the actor so we don't need to 
        // write it in each constructor
        void Setup(string Model, Vector3 Position)
        {
            this.model = new IEModel(this);
            this.Model = Model;

            this.Transform.Position = Position;
            this.Transform.Scale = Vector3.One;
            this.Transform.EulerRotation = Vector3.Zero;
        }

        public override void Draw()
        {
            ModelValue.World = MathUtil.CreateWorldMatrix(Transform.Position, Transform.Rotation, Transform.Scale);
            ModelValue.Draw();
        }

        public override void SaveSerializationData(SerializationData Data)
        {
            base.SaveSerializationData(Data);

            Data.AddData("Actor.Position", Transform.Position);
            Data.AddData("Actor.Rotation", Transform.EulerRotation);
            Data.AddData("Actor.Scale", Transform.Scale);
            Data.AddData("Actor.Model", Model);
        }

        public override void LoadFromSerializationData(SerializationData Data)
        {
            base.LoadFromSerializationData(Data);

            this.Transform.Position = Data.GetData<Vector3>("Actor.Position");
            this.Transform.EulerRotation = Data.GetData<Vector3>("Actor.Rotation");
            this.Transform.Scale = Data.GetData<Vector3>("Actor.Scale");

            string loadedModelName = Data.GetData<string>("Actor.Model");
            this.modelName = loadedModelName;

            foreach (GameScreen screen in Engine.GameScreens)
                foreach (Component component in screen.Components)
                    if (component.Name == loadedModelName)
                        this.model = (IEModel)component;
        }
    }
}
